package com.itboye.mengwu.buy.ui;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.adapter.AllOrderQueryAdapter;
import com.itboye.mengwu.data.model.OrderBean;
import com.itboye.mengwu.data.model.OrderQueryModel;
import com.itboye.mengwu.data.presenter.OrderPresenter;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

public class QueryAllOrder extends BaseActivity implements Observer,
		IXListViewListener {
	TextView txt_title;
	OrderPresenter orderPresenter;
	private int pageNo = 1;
	private ArrayList<OrderQueryModel.OrderList> orderQueryModel = new ArrayList<OrderQueryModel.OrderList>();

	XListView xlistView;
	String uid;
	private OrderBean model;
	private AllOrderQueryAdapter adapter;
	ImageView back;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_allorder;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub

		uid = SPUtils.get(this, null, SpPublicParams.UID, "") + "";
		adapter = new AllOrderQueryAdapter(orderQueryModel, this);
		xlistView.setAdapter(adapter);
		txt_title.setText("全部订单");
		showProgressDialog("加载中", true);
		xlistView.setXListViewListener(this);
		xlistView.setPullLoadEnable(true);
		xlistView.setPullRefreshEnable(true);
		xlistView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(QueryAllOrder.this,
						AllOrderDetail.class);
				intent.putExtra("model", orderQueryModel.get(position - 1));
				intent.putExtra("id", orderQueryModel.get(position - 1).getId());
				startActivity(intent);
			}
		});
		orderPresenter = new OrderPresenter(this);
		model = new OrderBean();
		model.setStatus("0");
		model.setUid(uid);

		model.setPageNo(pageNo + "");
		orderPresenter.getInfo(model);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (pageNo == 1) {
				orderQueryModel.clear();
			}
			orderQueryModel.addAll(((OrderQueryModel) entity.getData())
					.getList());
			adapter.notifyDataSetChanged();
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
		stopLoad();
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		pageNo = 1;
		model.setStatus("0");
		model.setUid(uid);
		model.setPageNo(pageNo + "");
		orderPresenter.getInfo(model);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		pageNo++;
		model.setStatus("0");
		model.setUid(uid);
		model.setPageNo(pageNo + "");
		orderPresenter.getInfo(model);
	}

	public void stopLoad() {
		xlistView.stopLoadMore();
		xlistView.stopRefresh();
	}
}
